package com.ktjy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {
    private static SqlSessionFactory factory;
    static {

        InputStream is= null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        factory=new SqlSessionFactoryBuilder().build(is);
    }
    public static SqlSession createSqlSession(){
        return factory.openSession(false);
    }
    public static void closeSqlSession(SqlSession sqlsession){
        if(sqlsession!=null){
            sqlsession.close();
        }
    }
}
